package com.example.smart2.ui;

import android.graphics.Color;
import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import com.example.smart2.R;
import com.example.smart2.adapter.AppAdapter;
import com.example.smart2.model.AppModel;
import com.example.smart2.network.ApiService;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class DashboardFragment extends Fragment {

    private ListView type;
    private RecyclerView ryApp;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View root=inflater.inflate(R.layout.fragment_dashboard, container, false);
        type=(ListView) root.findViewById(R.id.listView);
        ryApp=(RecyclerView) root.findViewById(R.id.recyclerView);
        ryApp.setLayoutManager(new GridLayoutManager(getActivity(),3));

        getAppList2();
        return root;
    }

    private void getAppList2(){
        Retrofit retrofit=new Retrofit.Builder()
                .baseUrl("http://124.93.196.45:10001")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        ApiService api=retrofit.create(ApiService.class);
        api.getAppList2().enqueue(new Callback<AppModel>() {
            @Override
            public void onResponse(Call<AppModel> call, Response<AppModel> response) {
                HashMap<String,AppModel> hashMap=new HashMap<>();
                for (int i=0;i<response.body().getRows().size();i++){
                    AppModel.RowsBean rowsBean=response.body().getRows().get(i);
                    if(hashMap.containsKey(rowsBean.getServiceType())){
                        hashMap.get(rowsBean.getServiceType()).getRows().add(rowsBean);
                    } else{
                        AppModel model=new AppModel();
                        model.getRows().add(rowsBean);
                        hashMap.put(rowsBean.getServiceType(),model);
//                        response.body().getRows().add(rowsBean);
//                        hashMap.put(rowsBean.getServiceType(),response.body());
                    }
                }
                List<String> list=new ArrayList<>();
                for (String type:hashMap.keySet()){
                    list.add(type);
                }
                ArrayAdapter<String> arrayAdapter=new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_1,list);
                type.setAdapter(arrayAdapter);

                type.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    private int i=0;
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        type.getChildAt(i).setBackgroundColor(getResources().getColor(R.color.bg));
                        view.setBackgroundColor(Color.WHITE);
                        i=position;
                        ryApp.setAdapter(new AppAdapter(getActivity(),hashMap.get(list.get(position))));
                    }
                });
                type.post(new Runnable() {
                    @Override
                    public void run() {
                        type.getChildAt(0).setBackgroundColor(Color.WHITE);
                        if(list.size()>0){
                            ryApp.setAdapter(new AppAdapter(getActivity(),hashMap.get(list.get(0))));
                        }
                    }
                });
            }

            @Override
            public void onFailure(Call<AppModel> call, Throwable throwable) {

            }
        });
    }
}